package bolangit.test.service.imple;

import bolangit.test.dao.RentCarRecordMapper;
import bolangit.test.exception.AjaxException;
import bolangit.test.model.RentCarRecord;
import bolangit.test.model.RentCarRecordExample;
import bolangit.test.service.RentCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author lxr
 * @date 2019/2/27
 * @email 200890949@qq.com
 * @decreption 租车记录
 */
@Service
public class RentarServiceImpl implements RentCarService {
    @Autowired
    private RentCarRecordMapper mapper;

    @Override
    public boolean rentCar(String location, String cardNum, String user) {
        Boolean key = true;
        RentCarRecord rentCarRecord = new RentCarRecord();
        rentCarRecord.setId(UUID.randomUUID().toString());
        rentCarRecord.setCardNum(cardNum);
        rentCarRecord.setStartLocal(location);
        rentCarRecord.setStartTime(new Date());
        rentCarRecord.setRecordStartTime(new Date());
        rentCarRecord.setCreateUser(user);
        try {
            mapper.insert(rentCarRecord);
        }catch (Exception e){
            key = false;
            throw new AjaxException("出现错误");
        }
        return key;
    }

    @Override
    public RentCarRecord getRecordByKey(String id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(RentCarRecord rentCarRecord) {
        mapper.updateByPrimaryKey(rentCarRecord);
    }

    @Override
    public List<RentCarRecord> get(String user) {
        RentCarRecordExample example = new RentCarRecordExample();
        RentCarRecordExample.Criteria criteria = example.createCriteria();
        criteria.andCreateUserEqualTo(user);
        return mapper.selectByExample(example);
    }

    @Override
    public boolean del(String id) {
        Boolean key = true;
        try {
            mapper.deleteByPrimaryKey(id);
        }catch (Exception e){
            key = false;
            throw new AjaxException("出现错误");
        }
        return key;
    }
}
